﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dbPoll_Application.Models.Poll>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	SetSession
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <div class="pageHeadingContainer"><h2 class="pageHeading">Set Session Location</h2></div>
    <div id="contentContainer">
        <%= Html.HiddenFor(m => m.pollID) %>
        <%= Html.HiddenFor(m => m.name) %>
        <%= Html.HiddenFor(m => m.latitude) %>
        <%= Html.HiddenFor(m => m.longitude) %>
        <div class="respondTitle"><div class="respondTitlePadding">Session Location</div></div>
        <div id="hierarchyContainer">
            <div id="respondQuestionContainerPadding">
                <div id="respondOptionsContainer">
                    <div id="setSession"></div>
                    <div id="setPollLocationButtons">
                        <input type="button" id="setPollLocationOK" value="OK" />
                        <input type="button" id="setPollLocationCancel" value="Cancel" /><span id="setPollLocationErrorMessage"></span>
                        <div class="right">
                        <label for="setSessionLatitude">Latitude:</label><input type="text" id="setSessionLatitude" name="setLatitude" />
                        <label for="setSessionLongitude">Longitude:</label><input type="text" id="setSessionLongitude" name="setLongitude" />
                        <input type="button" id="setSessionLocation" value="Change" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            var pollLocation = new google.maps.LatLng($('#latitude').val(), $('#longitude').val());
            var myOptions = {
                zoom: 8,
                center: pollLocation,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("setSession"), myOptions);
            var marker = new google.maps.Marker({
                position: pollLocation,
                map: map,
                title: $('#name').val(),
                draggable: true
            });
            $('#setSessionLocation').click(function() {
                $('#setPollLocationErrorMessage').text("");
                if (isNaN($('#setSessionLatitude').val()) || isNaN($('#setSessionLatitude').val()) || $('#setSessionLatitude').val() == "" || $('#setSessionLongitude').val() == "") {
                    $('#setPollLocationErrorMessage').text("Invalid location");
                }
                else {
                    var pollLocation = new google.maps.LatLng($('#setSessionLatitude').val(), $('#setSessionLongitude').val());
                    marker.setPosition(pollLocation);
                    map.setCenter(pollLocation);
                }
            });
            $('#setPollLocationCancel').click(function() {
                window.location.href = "/Poll/Questions?pollID=" + $('#pollID').val() + "&pollName=" + $('#name').val();
            });
            $('#setPollLocationOK').click(function() {
                $.ajax({
                    type: 'POST',
                    url: "/Poll/SetSession",
                    data: { pollID: $('#pollID').val(), latitude: marker.getPosition().lat(), longitude: marker.getPosition().lng() },
                    success: function() {
                        window.location.href = "/Poll/Run?pollID=" + $('#pollID').val();
                    }
                });
            });
        </script>
    </div>

</asp:Content>
